package someTestExcemple.APITest;

import DTO.Student;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class ArraysTest {
    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(Arrays.toString(arr)); //返回数组内容 -String
          int arr1[] = Arrays.copyOfRange(arr,0,3); //拷贝一个数组 包前不包后
        System.out.println(Arrays.toString(arr1));
        int[] arr2 = Arrays.copyOf(arr,20); //拷贝一个数组,指定新数组长度，通常用于数组扩容
        System.out.println(Arrays.toString(arr2));
        double[] prices ={100,16,219.5,68.8};
        Arrays.setAll(prices,i->prices[i]*0.9); //把数组中的数据改为数据再存回原来的数组
        System.out.println(Arrays.toString(prices));
        Arrays.sort(prices); //默认升序排序
        System.out.println(Arrays.toString(prices));
        Student[] students = new Student[4];
        students[0] = new Student("tom",12);
        students[1] = new Student("jack",6);
        students[2] = new Student("rose",20);
        students[3] = new Student("lucy",2);
        Arrays.sort(students); //因为Student实现了Comparable接口，所以可以进行排序
        System.out.println(Arrays.toString(students));
        //还可以用Comparator接口进行排序,匿名内部类对象,制定排序规则
        //public static <T> void sort(T[] a, Comparator<? super T> c)
        Arrays.sort(students,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        //Arrays.sort(students, (o1, o2) -> o1.getName().compareTo(o2.getName()));
        //Arrays.sort(students, Comparator.comparing(Student::getName));
        System.out.println(Arrays.toString(students));




    }
}
